package com.cndw.kungfu.event;

import com.cndw.asyncsync.DataSyncServer;
import com.cndw.kungfu.comm.AccessInspection;
import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.config.SystemCache;
import com.cndw.kungfu.core.LogSyncServer;
import com.cndw.kungfu.event.model.BusinessNoticeHandler;
import com.cndw.kungfu.event.model.IntervalBroadCastHandle;
import com.cndw.kungfu.event.model.MJonlineHandler;
import com.cndw.kungfu.event.model.SystemLoopHandler;
import com.cndw.kungfu.event.model.WorldBossHandler;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.ActivitySerialize;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataSerialize;
import com.cndw.kungfu.model.activity.domain.BalefileStaticeData;
import com.cndw.kungfu.model.activity.domain.KeyChest;
import com.cndw.kungfu.model.activity.domain.RateChest;
import com.cndw.kungfu.model.goods.domain.SysModeAwards;
import com.cndw.kungfu.model.pve.CityDefensesBattle;
import com.cndw.kungfu.model.pvp.api.ApiCore;
import com.cndw.kungfu.model.pvp.camp.CampWar;
import com.cndw.kungfu.model.pvp.campcross.CampCrossCore;
import com.cndw.kungfu.model.pvp.war.WarCorePross;
import com.cndw.kungfu.model.siege.domain.SiegeWar;

/**
 * 系统启动加载的任务
 * 
 * @author fantadust
 * @date 2011-5-19 下午07:46:09
 */
public class GameStartHandler extends BaseHandler implements Runnable {
	private boolean sysClosed;

	public GameStartHandler(boolean sysClosed) {
		this.sysClosed = sysClosed;
	}

	@Override
	public void run() {
		try {
			/**
			 * 初始化全部模块缓存
			 */
			Context.getCacheCenter().cacheInit();
			/**
			 * 加载访问控制
			 */
			AccessInspection.reloadLockIp();
			/**
			 * 10秒后启动公告
			 */
			BusinessNoticeHandler.runBoss();

			/**
			 * 从数据库初始化队列事件
			 */
			Context.getQueueServer().initQueueMapOnServerStart();

			int initialDelay = 30 - TimeProcesser.getUnixTime() % 30;

			/**
			 * 每30秒执行一次系统维护
			 */
			Context.getTimerServer().scheduleAtFixedRate(new SystemLoopHandler(), initialDelay, SystemLoopHandler.delaySeconds);

			/**
			 * 围城每天启动
			 */
			SiegeWar.addBroadcast();

			/**
			 * 每分钟同步3000次数据库,每次同步10条
			 */
			DataSyncServer.getInstance().setSpeedAndStep(ConstantsConfig.getSysAysncSpeedMinute(), ConstantsConfig.getSysAysncThreads());
			/**
			 * 每分钟同步2万次日志 每次同步100条
			 */
			LogSyncServer.getInstance().setSpeedAndStep(12000, 100);
			/**
			 * 武林大会发奖
			 */
			DataSerialize.getInstance().getChallengeData().check();
			DataSerialize.getInstance().getChallengeData().refreashAward();
			/**
			 * 启动野外boss刷新进程
			 */
			WorldBossHandler.runBoss();

			/**
			 * 启动间隔广播
			 */
			IntervalBroadCastHandle.runBoss();
			/**
			 * 每1分钟一次优化异步队列检测
			 */
			Context.getTimerServer().scheduleAtFixedRate(new IntelligenceOptimizeHandler(), IntelligenceOptimizeHandler.delaySeconds);

			SystemCache.getInstance();
			DataSerialize.getInstance();
			ActivitySerialize.getInstance();

			KeyChest.reload();

			RateChest.load();

			SysModeAwards.reload();

			ConstantsSystem.init();

			/**
			 * 襄阳保卫战
			 */
			CityDefensesBattle.runBoss();
			/**
			 * 门派战的启动
			 */
			CampWar.runBoss();

			hoildayEvent();

			QQDC.getInstance();

			WarCorePross.runBoss();

			crossEvent();

			/**
			 * 每30秒上传一次MJ在线
			 */
			Context.getTimerServer().scheduleAtFixedRate(new MJonlineHandler(), initialDelay, 60);

			ConstantsConfig.setSysClosed(sysClosed);

			ConstantsSystem.setCloseState(0);

			ConstantsSystem.setStartTime(TimeProcesser.getUnixTime());

			LogUtil.getMain().info("GameStartHandler success ..........");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void hoildayEvent() {
		// ZongZiSteamer.runBoss();//粽子启动
		/**
		 * 主播献花日榜发奖
		 */
		// MMFlowerData.getInstance().runAward();
		
		//PendantHandler.runBoss();//满城南瓜
	}

	private void crossEvent() {
		if (!ApiCore.isCrossServer()) {
			return;
		}
		CampCrossCore.runBoss();
		BalefileStaticeData.runBoss();
		// MMFlowerCross.runBoss();
	}

}
